//
//  XOWMPWDViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMPWDViewController.h"
#import "XOWMPWDView.h"
#import "XOWMRequest.h"
@interface XOWMPWDViewController ()<YJDPWDViewDelegate,YJDRequestDelegate>
@property (nonatomic, strong) XOWMPWDView *pwdView;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, strong) XOWMRequest *request;
@property (nonatomic, assign) YJDPWDViewEvent event;
@property (nonatomic, strong) NSDictionary *accountDict;

@end

@implementation XOWMPWDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    // Do any additional setup after loading the view.
}

- (XOWMPWDView *)pwdView
{
    if (!_pwdView)
    {
        _pwdView = [[XOWMPWDView alloc] init];
        _pwdView.delegate = self;
        [self.view addSubview:_pwdView];
        _pwdView.c_center.equal(self.view);
        _pwdView.c_size.equal(_pwdView.bounds.size);
    }
    return _pwdView;
}

- (XOWMRequest *)request
{
    if (!_request)
    {
        _request = [[XOWMRequest alloc]init];
        _request.delegate = self;
    }
    return _request;
}

#pragma mark - YJDPWDViewDelegate
- (void)yjd_actionForYJDPWDViewWithEvent:(YJDPWDViewEvent)event dict:(NSDictionary *)dict
{
    self.event = event;
    if (event == YJDPWDViewEventBack)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (event == YJDPWDViewEventFindPWD)
    {
        if ([self.pwdView.firstTF.text length] < 1) return [CCAlert showMessage:@"用户名不能为空" hidden:nil];
        else if ([self.pwdView.thirdTF.text length] < 1) return [CCAlert showMessage:@"密码不能为空" hidden:nil];
        else if (![self.pwdView.firstTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"用户名只能包含数字和字母" hidden:nil];
        else if (![self.pwdView.secondTF.text isEqualToString:self.code]) return [CCAlert showMessage:@"验证码错误" hidden:nil];
        else if (![self.pwdView.thirdTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"密码只能包含数字和字母" hidden:nil];
        
        [CCHud startHud];
        self.accountDict = dict;
        [self.request yjd_findPWDRequest:dict];
    }
    else if (event == YJDPWDViewEventResetPWD)
    {
        if ([self.pwdView.firstTF.text length] < 1) return [CCAlert showMessage:@"原密码不能为空" hidden:nil];
        else if ([self.pwdView.secondTF.text length] < 1) return [CCAlert showMessage:@"新密码不能为空" hidden:nil];
        else if ([self.pwdView.thirdTF.text length] < 1) return [CCAlert showMessage:@"请再一次确认新密码" hidden:nil];
        else if (![self.pwdView.firstTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"用户名只能包含数字和字母" hidden:nil];
        else if (![self.pwdView.secondTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"密码只能包含数字和字母" hidden:nil];
        else if (![self.pwdView.thirdTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"密码只能包含数字和字母" hidden:nil];
        else if (![self.pwdView.secondTF.text isEqualToString:self.pwdView.thirdTF.text]) return [CCAlert showMessage:@"两次输入的新密码不一致" hidden:nil];
        else if ([self.pwdView.firstTF.text isEqualToString:self.pwdView.secondTF.text] || [self.pwdView.firstTF.text isEqualToString:self.pwdView.thirdTF.text])
            return [CCAlert showMessage:@"新密码不能与旧密码一致" hidden:nil];
        
        if (helperManager().getPara().accountID)
        {
            self.accountDict = @{@"name":helperManager().getPara().account,@"password":[dict valueForKey:@"password"]};
        }
        [CCHud startHud];
        [self.request yjd_resetPWDRequest:dict];
    }
    else if (event == YJDPWDViewEventVerify)
    {
        if ([self.pwdView.firstTF.text length] < 1) return [CCAlert showMessage:@"用户名不能为空" hidden:nil];
        else if (![self.pwdView.firstTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"用户名只能包含数字和字母" hidden:nil];
        else
        {
            [self.request yjd_verifyCodeRequest:dict];
        }
        return;
    }
    
}

#pragma mark - YJDRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict
{
    //关闭菊花
    [CCHud stopHud];    
    if (!dict)
        return;
    
    int code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [dict valueForKey:@"info"];
    //成功
    if (code == 100)
    {
        if (self.event == YJDPWDViewEventVerify)
        {
            self.code = info;
            return;
        }
        helperManager().insertAccount(self.accountDict);
        [CCAlert showMessage:info hidden:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    //失败
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.pwdView.backBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_返回_Btn":@"back";
    if (self.type == YJDPWDViewTypeFindPWD)
    {
        self.pwdView.findBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_找回密码_Btn":@"确定";
        self.pwdView.titleIMG.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_找回密码_title":@"找回密码";
    }
    else
    {
        self.pwdView.findBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_修改密码_Btn":@"确定修改";
        self.pwdView.titleIMG.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_修改密码_title":@"修改密码";
    }
    self.pwdView.type = self.type;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_checkDefualtSetting {
    NSLog(@"Get Info Success");
}
@end
